hysop.tools.htypes module

class hysop.tools.htypes.InstanceOf(cls)[source]

Bases: object

match_instance(obj)[source]
hysop.tools.htypes.check_instance(val, cls, allow_none=False, check_kwds=True, **kargs)[source]

Raise a TypeError if val is not an instance of cls. cls can be a tuple of types like isinstance(…) capabilities.

If allow_none is False and val is None, an exception is raised.

This function is specialized for some types like array like and dictionaries. For array likes and dicts an additional values=val_cls parameter can be passed to check contained values type.

For dictionnaries an additional keys=key_cls parameter can be passed to check contained keys type in addition to values.

val_cls and key_cls have the same format as cls (ie. can be a tuple of valid types).

For iterables you can check size, minsize, maxsize, min_value and max_value using keys:

‘size’, ‘minsize’, ‘maxsize’, ‘minval’, ‘maxval’.

For np.ndarrays, in addition to the iterables keys you can check ndim, shape and dtype using the keys ‘ndim’, ‘shape’ and ‘dtype’. If shape and size are specified at the same time they should be compatible.

If python builtin variable __debug__ is set to True (ie. python was run with -O option or PYTHONOPTIMIZE environment variable was set to a non-empty string), the check is disabled (like asserts) and this method returns immediately.

hysop.tools.htypes.extend_array(value, dim, dtype=None, fillval=None)[source]

Extend value to have specified dimension. If fillval is None, the last value is used to complete the array. Returns a np.ndarray of specified dtype and size dim.

hysop.tools.htypes.first_not_None(a0, *args)[source]

Return the first argument that is not None. If only one argument is given it is considered as an iterable, else iterates on (a0,)+args. If all arguments are None, return None.

hysop.tools.htypes.to_frozenset(arg, cast=None)[source]
Convert any of the following types:

scalar,tuple,list,set,frozenset,np.ndarray

to a frozenset.

hysop.tools.htypes.to_list(arg, cast=None)[source]
Convert any of the following types:

scalar,tuple,list,set,frozenset,np.ndarray

to a list.

hysop.tools.htypes.to_set(arg, cast=None)[source]
Convert any of the following types:

scalar,tuple,list,set,frozenset,np.ndarray

to a set.

hysop.tools.htypes.to_tuple(arg, cast=None)[source]
Convert any of the following types:

scalar,tuple,list,set,frozenset,np.ndarray

to a tuple.